package com.kfm.base.net;

import java.io.IOException;
import java.net.*;

public class UDPClient {

    public static void main(String[] args) {
        try {
//            InetAddress inetAddress = InetAddress.getLocalHost();
            InetAddress inetAddress = InetAddress.getByName("10.0.4.140");
            int port = 8087;
            // Client
            DatagramSocket client = new DatagramSocket();
            System.out.println(client.getPort());
            System.out.println(client.getLocalPort());
            String msg = "hello";
            DatagramPacket packet = new DatagramPacket(msg.getBytes(), msg.getBytes().length, inetAddress, port);
            client.send(packet);

            System.out.println("发送成功");

            // 创建线程接收消息

        } catch (SocketException e) {
            throw new RuntimeException(e);
        } catch (UnknownHostException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
